import prefix from "@/utils/prefix.js";
import resDeal from '@/utils/responseDeal.js'
export default (config) => {
	try {
		config.url = prefix + config.url;
		config.data = config.data || {}
		if(config.pagination==true){
			config.data.page = config.data.page || 1;
			config.data.limit = config.data.limit || 12;
		}
		
		if (config.token == true) {
			config.header = {
				Authorization: uni.getStorageSync("Authorization") || ""
			}
		}

		return new Promise(function(resolve, reject) {
			try {
				uni.request({
					url: config.url,
					method: config.method,
					header: config.header,
					data: config.data,
					success: (r) => { //数据获取成功
						const {
							data: res
						} = r;
						resolve(resDeal(res));
					},
					fail: (err) => { //失败操作
						uni.showToast({
							title: "服务器异常",
							icon: "error",
							duration: 2000
						});
						uni.redirectTo({
							url: '/pages/Login/Login',
						});
					}
				})
			} catch (e) {
				console.log(e);
				reject(e);
			}
		})
	} catch (e) {
		console.log(e)
	}
};


